package com.cjm.one.goods.apps;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjm.one.common.exception.GlobalException;
import com.cjm.one.common.result.Result;
import com.cjm.one.common.result.ResultCodeEnum;
import com.cjm.one.common.utils.AuthCustomerContextHolder;
import com.cjm.one.config.GreenService;
import com.cjm.one.goods.service.GoodsCommentService;
import com.cjm.one.model.goods.dto.GoodsCommentDto;
import com.cjm.one.model.goods.entity.GoodsComment;
import com.cjm.one.model.goods.entity.GoodsCommentTag;
import com.cjm.one.model.goods.vo.GoodsCommentCountVo;
import com.cjm.one.model.goods.vo.GoodsCommentTagVo;
import com.cjm.one.model.goods.vo.GoodsCommentVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/app/goods/comment")
@Tag(name = "商品评论", description = "商品评论")
public class AppGoodsCommentController {

    @Resource
    private GoodsCommentService goodsCommentService;
    @Resource
    private GreenService greenService;
    @GetMapping("/list")
    @Operation(summary = "商品评论列表")
    public Result<Page<GoodsCommentVo>> list(
            @RequestParam(value = "current", defaultValue = "1") Integer current,
            @RequestParam(value = "size", defaultValue = "10") Integer size,
            @RequestParam(value = "goodsId") Long goodsId,
            @RequestParam(value = "sortOrder", required = false) String sortOrder,
            @RequestParam(value = "tagIds", required = false) Long tagIds) {

        ArrayList arrayList = new ArrayList();
        if(tagIds != null) {
            arrayList.add(tagIds);
        }
        Page<GoodsCommentVo> page = goodsCommentService.getCommentPage(
                new Page<GoodsCommentVo>(current, size),
                goodsId,
                sortOrder,
                arrayList
        );
        return Result.ok(page);
    }
    @PostMapping("/save")
    @Operation(summary = "保存商品评论")
    public Result<Boolean> save(@RequestBody List<GoodsCommentDto> goodsCommentDto) {

        Long id = AuthCustomerContextHolder.getAuthContextCustomer().getId();
        log.info("保存商品评论",goodsCommentDto);
        StringBuilder contentBuilder = new StringBuilder();
        goodsCommentDto.stream().forEach(item -> {
            item.setUserId(id);
            contentBuilder.append(item.getContent() != null ?     // 处理null值
                    item.getContent() : "");
        });
        String combinedContent = contentBuilder.toString();
        boolean b = greenService.textModeration(combinedContent);
        if (!b) {
            throw new GlobalException(ResultCodeEnum.COMMENT_SENSITIVE);
        }
        return Result.ok(goodsCommentService.saveGoodsComment(goodsCommentDto));
    }
    @GetMapping("/getTagById")
    @Operation(summary = "根据商品id获取商品评论的标签")
    public Result<GoodsCommentCountVo> getTagById(@RequestParam("id") Long id) {
        log.info("根据id获取商品评论");
        return Result.ok(goodsCommentService.getCommentTagById(id));
    }
    @GetMapping("/top3")
    @Operation(summary = "获取热门前三评论")
    public Result<List<GoodsComment>> getTopComments(
            @RequestParam(value = "goodsId") Long goodsId) {
        List<GoodsComment> comments = goodsCommentService.lambdaQuery()
                .eq(GoodsComment::getGoodsId, goodsId)
                .eq(GoodsComment::getIsDeleted, 0)
                .eq(GoodsComment::getStatus, 1)
                .select(GoodsComment::getId,
                        GoodsComment::getContent,
                        GoodsComment::getScore,
                        GoodsComment::getNickname,
                        GoodsComment::getAvatarUrl,
                        GoodsComment::getCreateTime)
                .orderByDesc(GoodsComment::getScore)
                .orderByDesc(GoodsComment::getCreateTime)
                .last("LIMIT 3")
                .list();
        return Result.ok(comments);
    }

    @GetMapping("/tags")
    @Operation(summary = "获取所有评论标签")
    public Result<List<GoodsCommentTag>> getTags() {
        List<GoodsCommentTag> tags = goodsCommentService.getTags();
        return Result.ok(tags);
    }

}
